package cn.bookcycle.panda.user.dao.impl;

import cn.bookcycle.panda.user.dao.UserDao;
import cn.bookcycle.panda.user.pojo.DO.UserDO;
import cn.bookcycle.panda.user.util.Dao;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("userDaoMongo")
public class UserDaoImpl implements UserDao {
    @Autowired
    Dao<Object> userDao;
    @Override
    public Object getUserById(String userId) {
        UserDO user=new UserDO();
        user.setId(userId);
        try {
            String result=userDao.daoMethod("user","find",user);
            Object object= JSONArray.fromObject(result).get(0);
            if("null".equals(object.toString())){
                return null;
            }
            return object;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public boolean updateUser(Object userDO) {
        try{
            userDao.daoMethod("user","update", userDO);
            return true;
        }catch (Exception e){
            return false;
        }
    }
    @Override
    public boolean saveUser(Object userDO) {
        try{
            userDao.daoMethod("user","insert", userDO);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @Override
    public List listUser(Object userDO) {
        try {
            String result=userDao.daoMethod("user","find","ExactMatch",userDO);
            JSONArray jsonArray = JSONArray.fromObject(result);
            List userDOList=(List)JSONArray.toCollection(jsonArray,userDO.getClass());
            return  userDOList;
        }catch (Exception e){
            return null;
        }
    }
}
